﻿using System; 

namespace Plugin
{
    public class %MacroName% : MarshalByRefObject //Do not change this line
    {
    
        public int Version = 4;
        

        public string Execute(ref float[] Predictions, float[] PredictionCorrectionSlopes, float[] PredictionCorrectionIntercepts, string MeasurementNumberAndCount,
                            String ModelName, float[] ModelFits,
                            String GPSLocation, String FileName, DateTime CreatedDateTime,
                            String SampleNumberAndLotNumber, int SampleNumber, int LotNumber, float Prediction1LotAverage, float Prediction1LotStandardDeviation,
                            int aFirstWavelength, float[] AbsorbanceSpectra, float[] FirstDerivativeSpectra, float[] SecondDerivativeSpectra, float[] MacroSpectra)
        {        
      
            String result = "";
            
            try
            {
                    
                //The first wavelength of the regression coefficents
                int FirstWavelength = 879; 
                
                //Note: aFirstWavelength is the first wavelength of the spectrometer.  Possibly as low as 285, but not always 285
                //calculate how far into the spectra array to begin appling recos 
                int StartingPixel = ((FirstWavelength - aFirstWavelength) / 3);
                
                //Paste in regression coefficents from your external software 
                //Note: The "f" after each number is required.
                float[] RegressionCoefficients = new float[] { 20.60395452f, 174.0394421f, 303.397235f, 277.4527979f, 111.4293517f, 97.60814479f, 260.9577949f, 112.1365307f, 290.5233867f, 232.4661888f, 261.8132817f, 163.989211f, 226.205312f };
                
                //Paste in the intercept or B0 from your external software 
                float B0 = 15.1234f;

                //This can be used to verify your logic in Model Builder before using the device.
                //This test spectra is SecondDerivativeSpectra sample #? should predict a value of ? (Use your own)
                //Note: This spectra is the full 285-1200 range, and the "f" after each number is required.
                //float[] TestDerivativeSpectra = new float[]  { 264.8293679f, 303.191969f, 291.2900083f, 197.4823173f, 13.50887717f, 133.5156172f, 197.2044918f, 212.6002997f, 145.7799746f, 142.7957991f, 218.6463483f, 315.1948998f, 231.2303155f, 265.246123f, 311.4394257f, 123.8184636f, 81.51302999f, 27.76167371f, 3.086782037f, 281.9000408f, 184.22185f, 226.1417251f, 100.1110307f, 140.0359441f, 150.9027523f, 250.6475549f, 188.6911006f, 200.2389804f, 8.827240387f, 21.38610115f, 130.356799f, 294.3200656f, 42.03121842f, 112.5979f, 78.60796078f, 206.9085999f, 211.6050835f, 14.87269864f, 290.7613001f, 42.40088035f, 14.63745296f, 249.3499696f, 32.49685489f, 43.89384707f, 72.0594242f, 60.98281264f, 16.79617258f, 61.46722764f, 117.7098934f, 262.8775851f, 208.1215788f, 133.9692673f, 157.7468479f, 154.7337462f, 47.93635659f, 121.5132501f, 139.6641472f, 29.63548663f, 70.98010859f, 42.9911472f, 115.4048548f, 80.9237401f, 195.499259f, 210.2523641f, 172.335225f, 57.45994959f, 268.325813f, 251.7990878f, 128.11138f, 259.6834116f, 86.67220249f, 290.8117129f, 214.6633173f, 152.6500321f, 282.8182413f, 121.789208f, 201.5878286f, 116.8706981f, 263.8092021f, 47.96214045f, 191.4889483f, 277.7666531f, 102.1687493f, 285.7426116f, 225.4848535f, 54.85393652f, 62.14841265f, 311.064914f, 306.2780643f, 14.93573852f, 171.6160265f, 241.8609234f, 284.1456428f, 47.90099179f, 156.4713204f, 29.69375122f, 36.96585831f, 312.8185209f, 25.55968923f, 208.1607949f, 34.34720525f, 18.55074516f, 143.169596f, 132.2388727f, 162.2503465f, 94.81053718f, 38.29065139f, 168.5652342f, 299.6499196f, 247.3444359f, 267.4149313f, 181.5428216f, 302.3169029f, 175.8236616f, 36.42411188f, 5.213653352f, 235.9124379f, 256.6577321f, 62.83614614f, 187.4685773f, 313.5515456f, 204.9596183f, 1.764401911f, 102.9762706f, 188.7913283f, 47.35617115f, 171.0041927f, 180.4961193f, 111.7369354f, 65.28172148f, 167.1406642f, 193.2233216f, 270.361883f, 18.29573744f, 54.82488412f, 3.152796109f, 264.9964587f, 203.7677601f, 303.099669f, 71.24091147f, 227.6500074f, 127.2350508f, 251.8318797f, 70.56882682f, 249.7787488f, 35.91377586f, 55.2510148f, 189.3827183f, 289.0579006f, 117.4773551f, 26.59141331f, 112.1845821f, 182.0817769f, 125.742356f, 174.8486562f, 144.4174787f, 243.0103808f, 165.2701062f, 188.6342056f, 279.2198556f, 31.13882077f, 46.6789183f, 247.3593364f, 67.76751603f, 205.3404851f, 191.9967997f, 26.94103439f, 17.79629192f, 199.9493289f, 266.6710429f, 28.22752425f, 133.5929991f, 228.2491622f, 276.6522229f, 131.4904408f, 133.5498568f, 42.04990305f, 55.35692213f, 190.7186289f, 174.6391531f, 242.7582692f, 43.63674706f, 129.357103f, 20.24657732f, 160.2803434f, 210.9845017f, 205.9851929f, 58.24607057f, 141.3248594f, 78.48467876f, 179.2587089f, 204.233317f, 35.13335498f, 288.2523815f, 190.5878799f, 297.2783588f, 112.0554755f, 306.8023434f, 179.6926001f, 256.3855737f, 153.8589911f, 5.695870796f, 250.0443671f, 125.0785072f, 59.73121302f, 199.4139613f, 221.390946f, 231.7365775f, 52.56914238f, 211.5412885f, 289.865906f, 134.2327997f, 45.75839749f, 7.286647396f, 99.70851372f, 0.48887933f, 41.34393749f, 301.4539438f, 317.2433939f, 71.35801427f, 96.26742208f, 200.1097481f, 12.19972424f, 79.53275859f, 20.89111309f, 168.4911628f, 233.5392043f, 213.4959861f, 38.89881588f, 48.9800377f, 155.1620933f, 182.5268239f, 24.35548864f, 186.9424556f, 73.35505538f, 49.53492599f, 70.24645695f, 146.4796255f, 3.580118395f, 270.4257078f, 50.45411295f, 157.6313006f, 207.7315113f, 97.32044043f, 235.0254036f, 166.9327327f, 85.41123398f, 260.0239166f, 279.3696635f, 310.4781957f, 232.7422052f, 243.6298235f, 221.4620719f, 298.5666059f, 290.8881577f, 160.5112113f, 205.0943914f, 281.5899363f, 92.74148851f, 303.4187658f, 160.3372179f, 48.51621455f, 130.3575064f, 123.4790118f, 194.1823074f, 98.11532722f, 181.6458393f, 208.4250321f, 58.68719775f, 264.0399381f, 66.75413054f, 189.179867f, 20.03379814f, 312.9036805f, 249.2346321f, 180.8190143f, 26.43567897f, 165.8474945f, 168.5458477f, 84.34770157f, 51.03261797f, 90.70589612f, 63.13111694f, 83.02405896f, 131.1438043f, 261.8731475f, 172.5985371f, 231.6834157f, 160.1114172f, 95.80578948f, 315.1028287f, 145.8339015f, 17.07599525f, 183.025363f, 287.9381985f, 46.28311294f, 241.3228405f, 222.5349143f, 6.362749294f, 213.1991462f, 8.373522513f, 274.8595266f, 307.4119478f, 22.34849725f, 181.6871991f };
                //Used to store the prediction as we build it.
                
                float macroPredictionValue = 0f;
                
                for (int pixel = 0; pixel < RegressionCoefficients.Length; ++pixel)
                {
                
                    //Uncomment this line before putting the macro on your device.
                    macroPredictionValue += SecondDerivativeSpectra[StartingPixel + pixel] * RegressionCoefficients[pixel];
               
                    //Uncomment this line to test the TestDerivativeSpectra above in Model Builder.
                    //macroPredictionValue += TestDerivativeSpectra[StartingPixel + pixel] * RegressionCoefficients[pixel];  
                }

                //Adds the B0 Intercept
                macroPredictionValue = macroPredictionValue + B0;
                
                //This error handling returns "friendly" errors instead of wild values.
                if (macroPredictionValue > 20)
                {
                    result = "High Error";
                }
                else if (macroPredictionValue < 0)
                {
                    result = "Low Err";
                }
                else 
                { 
                    result = Format(macroPredictionValue, 1);
                } 
            } 
            catch
            {
                result = "Macro Error"; 
            }
             
            return result;
        }
         

		public string Format(double aValue, int aDecimalPlaces)
        {
            if ((aValue > int.MaxValue) || (aValue < int.MinValue) || (aValue == 0))
            {
                if (aDecimalPlaces == 0)
                {
                    return "0";
                }
                else
                {
                    return "0." + new String('0', aDecimalPlaces);
                }
            }

            bool isNegative = aValue < 0;
            aValue = System.Math.Abs(aValue);

            double increased = System.Math.Round((aValue * System.Math.Pow(10, aDecimalPlaces))) / System.Math.Pow(10, aDecimalPlaces);

            double firstPart = (int)increased;
            double secondPart = (increased - firstPart);


            string result = "";
            
            if (aDecimalPlaces > 0)
            {
                string nv = ((new String('0', aDecimalPlaces)) + ((int)System.Math.Round(secondPart * System.Math.Pow(10, aDecimalPlaces)))).ToString();
                nv = nv.Substring(nv.Length - aDecimalPlaces, aDecimalPlaces);
                result = ((int)System.Math.Round(firstPart)).ToString() + "." + nv;
            }
            else
            {
                result = ((int)System.Math.Round(firstPart)).ToString();
            }


            if (isNegative)
            {
                result = "-" + result;
            }


            return result;
        }
    }
}
